package com.example.dw.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dw.entity.Order;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 订单Mapper接口
 * @author system
 * @since 2024-01-01
 */
@Mapper
public interface OrderMapper extends BaseMapper<Order> {

    /**
     * 根据订单ID获取订单详情（带关联信息）
     */
    @Select("SELECT o.*, u.username, " +
            "CASE WHEN o.orderOfType = 0 THEN c.courseName " +
            "     WHEN o.orderOfType = 1 THEN a.activityName " +
            "     ELSE NULL END as itemName " +
            "FROM `order` o " +
            "LEFT JOIN user u ON o.userid = u.userid " +
            "LEFT JOIN course c ON o.courseid = c.courseid " +
            "LEFT JOIN activity a ON o.sessionid = s.sessionid AND s.activityid = a.activityid " +
            "LEFT JOIN activity_session s ON o.sessionid = s.sessionid " +
            "WHERE o.orderid = #{orderid}")
    Order getOrderByIdWithDetails(@Param("orderid") Integer orderid);

    /**
     * 根据用户ID获取所有订单
     */
    @Select("SELECT o.*, u.username, " +
            "CASE WHEN o.orderOfType = 0 THEN c.courseName " +
            "     WHEN o.orderOfType = 1 THEN a.activityName " +
            "     ELSE NULL END as itemName " +
            "FROM `order` o " +
            "LEFT JOIN user u ON o.userid = u.userid " +
            "LEFT JOIN course c ON o.courseid = c.courseid " +
            "LEFT JOIN activity a ON o.sessionid = s.sessionid AND s.activityid = a.activityid " +
            "LEFT JOIN activity_session s ON o.sessionid = s.sessionid " +
            "WHERE o.userid = #{userid} " +
            "ORDER BY o.createdAt DESC")
    List<Order> getAllOrdersByUser(@Param("userid") Integer userid);

    /**
     * 根据用户ID分页获取订单
     */
    @Select("SELECT o.*, u.username, " +
            "CASE WHEN o.orderOfType = 0 THEN c.courseName " +
            "     WHEN o.orderOfType = 1 THEN a.activityName " +
            "     ELSE NULL END as itemName " +
            "FROM `order` o " +
            "LEFT JOIN user u ON o.userid = u.userid " +
            "LEFT JOIN course c ON o.courseid = c.courseid " +
            "LEFT JOIN activity a ON o.sessionid = s.sessionid AND s.activityid = a.activityid " +
            "LEFT JOIN activity_session s ON o.sessionid = s.sessionid " +
            "WHERE o.userid = #{userid} " +
            "ORDER BY o.createdAt DESC " +
            "LIMIT #{offset}, #{size}")
    List<Order> getOrdersByUserPage(@Param("userid") Integer userid,
                                    @Param("offset") Integer offset,
                                    @Param("size") Integer size);

    /**
     * 根据用户ID获取订单总数
     */
    @Select("SELECT COUNT(*) FROM `order` WHERE userid = #{userid}")
    Integer getOrderCountByUser(@Param("userid") Integer userid);

    /**
     * 根据订单号获取订单
     */
    @Select("SELECT * FROM `order` WHERE reqsn = #{reqsn}")
    Order getOrderByReqsn(@Param("reqsn") String reqsn);

    /**
     * 根据用户ID和交易状态获取订单
     */
    @Select("SELECT o.*, u.username, " +
            "CASE WHEN o.orderOfType = 0 THEN c.courseName " +
            "     WHEN o.orderOfType = 1 THEN a.activityName " +
            "     ELSE NULL END as itemName " +
            "FROM `order` o " +
            "LEFT JOIN user u ON o.userid = u.userid " +
            "LEFT JOIN course c ON o.courseid = c.courseid " +
            "LEFT JOIN activity a ON o.sessionid = s.sessionid AND s.activityid = a.activityid " +
            "LEFT JOIN activity_session s ON o.sessionid = s.sessionid " +
            "WHERE o.userid = #{userid} AND o.trxstatus = #{trxstatus} " +
            "ORDER BY o.createdAt DESC")
    List<Order> getOrdersByUserAndStatus(@Param("userid") Integer userid,
                                         @Param("trxstatus") Integer trxstatus);

    /**
     * 根据用户ID和订单类型获取订单
     */
    @Select("SELECT o.*, u.username, " +
            "CASE WHEN o.orderOfType = 0 THEN c.courseName " +
            "     WHEN o.orderOfType = 1 THEN a.activityName " +
            "     ELSE NULL END as itemName " +
            "FROM `order` o " +
            "LEFT JOIN user u ON o.userid = u.userid " +
            "LEFT JOIN course c ON o.courseid = c.courseid " +
            "LEFT JOIN activity a ON o.sessionid = s.sessionid AND s.activityid = a.activityid " +
            "LEFT JOIN activity_session s ON o.sessionid = s.sessionid " +
            "WHERE o.userid = #{userid} AND o.orderOfType = #{orderOfType} " +
            "ORDER BY o.createdAt DESC")
    List<Order> getOrdersByUserAndType(@Param("userid") Integer userid,
                                       @Param("orderOfType") Integer orderOfType);

    /**
     * 更新订单支付状态
     */
    @Update("UPDATE `order` SET trxstatus = #{trxstatus}, trxid = #{trxid}, " +
            "chnltrxid = #{chnltrxid}, fintime = #{fintime}, payTime = NOW() " +
            "WHERE reqsn = #{reqsn}")
    int updateOrderPayStatus(@Param("reqsn") String reqsn,
                            @Param("trxstatus") Integer trxstatus,
                            @Param("trxid") String trxid,
                            @Param("chnltrxid") String chnltrxid,
                            @Param("fintime") String fintime);

    /**
     * 更新订单支付信息
     */
    @Update("UPDATE `order` SET package1 = #{package1}, code_url = #{codeUrl} " +
            "WHERE orderid = #{orderid}")
    int updateOrderPayInfo(@Param("orderid") Integer orderid,
                          @Param("package1") String package1,
                          @Param("codeUrl") String codeUrl);

    /**
     * 获取过期未支付订单
     */
    @Select("SELECT * FROM `order` WHERE trxstatus = 0 " +
            "AND TIMESTAMPDIFF(MINUTE, createdAt, NOW()) > validtime")
    List<Order> getExpiredOrders();

    /**
     * 批量更新过期订单状态
     */
    @Update("UPDATE `order` SET trxstatus = 2 WHERE trxstatus = 0 " +
            "AND TIMESTAMPDIFF(MINUTE, createdAt, NOW()) > validtime")
    int updateExpiredOrdersStatus();
} 